cmake_minimum_required(VERSION 3.18)

project(llvm_plugin)

# basic setup
set(LLVM_VERSION_FULL "llvm-16.0.6")
set(LLVM_VERSION 16)
set(LLVM_ROOT_DIR "/home/zzy/${LLVM_VERSION_FULL}/bin")
set(LLVM_INCLUDE_DIR "${LLVM_ROOT_DIR}/include/llvm")

# check include dir
if(NOT EXISTS "${LLVM_INCLUDE_DIR}")
message(FATAL_ERROR "${LLVM_INCLUDE_DIR} not found")
endif()

# check llvm config
if(EXISTS "${LLVM_ROOT_DIR}/lib/cmake/llvm/LLVMConfig.cmake")
message(STATUS "LLVMConfig.cmake found: ${LLVM_ROOT_DIR}/lib/cmake/llvm/LLVMConfig.cmake")
endif()

list(APPEND CMAKE_PREFIX_PATH "${LLVM_ROOT_DIR}/lib/cmake/llvm/")

find_package(LLVM ${LLVM_VERSION} REQUIRED CONFIG)
message(STATUS "LLVM MAJOR VERSION: ${LLVM_VERSION_MAJOR}")
message(STATUS "LLVM VERSION: ${LLVM_PACKAGE_VERSION}")
message(STATUS "LLVMConfig.cmake: ${LLVM_ROOT_DIR}")

message("LLVM STATUS:
  Definitions ${LLVM_DEFINITIONS}
  Includes    ${LLVM_INCLUDE_DIRS}
  Libraries   ${LLVM_LIBRARY_DIRS}
  Targets     ${LLVM_TARGETS_TO_BUILD}"
)

# Set the LLVM header and library paths
include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
link_directories(${LLVM_LIBRARY_DIRS})
add_definitions(${LLVM_DEFINITIONS})

# setup c++ 17
set(CMAKE_CXX_STANDARD 17 CACHE STRING "")

# Compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fdiagnostics-color=always")

# for llvm rtti
if(NOT LLVM_ENABLE_RTTI)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
endif()

# output directories
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")

include(AddLLVM)

# add sub directory
add_subdirectory(hello)
# add_subdirectory(hello_new)
add_subdirectory(PrintFunctionNames)
add_subdirectory(Bye)
